package org.springblade.tms.global.utils;

import org.springblade.common.constant.CommonConstant;
import org.springblade.system.feign.IDictBizClient;
import org.springblade.system.user.entity.User;
import org.springblade.system.user.enums.UserEnum;
import org.springblade.tms.config.SpringContextHolder;
import org.springblade.tms.global.constant.DictCodeConstant;
import org.springblade.tms.global.enums.UserTypeEnum;
import org.springblade.tms.module.userinfo.entity.TMUserInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @projectName: BladeX
 * @package: org.springblade.tms.global.utils
 * @className: 创建用户Util
 * @author: yr
 * @description:
 * @date: 2022/3/8 16:10
 */

@Component
public class CreateUserUtil {
	private static String tenantId;

	@Value("${tenant_id}")
	public void setSavePath(String tenantId) {
		this.tenantId = tenantId;
	}

	/**
	 * @param userInfo:
	 * @return User
	 * @author yr
	 * @description 通过TmUser新增用户
	 * @date 2022/3/8 16:15
	 */
	public static User createUser(TMUserInfo userInfo) {
		User user = new User();
		userInfoToUser(userInfo, user);
		return user;
	}

	private static void userInfoToUser(TMUserInfo userInfo, User user) {
		IDictBizClient iDictBizClient = SpringContextHolder.getBean(IDictBizClient.class);
		if (userInfo.getUserType().equals(UserTypeEnum.CHAUFFEUR.getValue())) {
			user.setUserType(UserEnum.APP.getCategory());
			user.setRoleId(iDictBizClient.getValue(DictCodeConstant.USER_SERVICE, DictCodeConstant.DRIVER_ROLE_ID,tenantId).getData());
		} else if (userInfo.getUserType().equals(UserTypeEnum.CARRIER.getValue())) {
			user.setUserType(UserEnum.WEB.getCategory());
			user.setRoleId(iDictBizClient.getValue(DictCodeConstant.USER_SERVICE, DictCodeConstant.CARRIER_ROLE_ID,tenantId).getData());
		}
		user.setPostId(iDictBizClient.getValue(DictCodeConstant.USER_SERVICE, DictCodeConstant.POST_ID,tenantId).getData());
		user.setDeptId(iDictBizClient.getValue(DictCodeConstant.USER_SERVICE, DictCodeConstant.DEPT_ID,tenantId).getData());
		user.setPhone(userInfo.getPhone());
		user.setPassword(CommonConstant.DEFAULT_PASSWORD);
		user.setAccount(userInfo.getPhone());
		user.setName(userInfo.getName());
		user.setTenantId(tenantId);
		user.setRealName(userInfo.getName());
	}

	/**
	 * @param userInfo:
	 * @return User
	 * @author yr
	 * @description 通过TmUser修改用户
	 * @date 2022/3/8 16:15
	 */
	public static User updateUser(TMUserInfo userInfo, User user) {
		user.setPhone(userInfo.getPhone());
		user.setAccount(userInfo.getPhone());
		user.setName(userInfo.getName());
		user.setRealName(userInfo.getName());
		return user;
	}
}
